import os
from abc import ABCMeta, abstractmethod

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.remote.webdriver import WebDriver

SELENIUM_GRID_URL = f"http://{os.getenv('IP')}:4444/wd/hub"
print(f"{SELENIUM_GRID_URL=}")


class Driver(metaclass=ABCMeta):
    @abstractmethod
    def initialize(self):
        """Initialize driver"""

    @abstractmethod
    def close(self):
        """Close driver"""


class RemoteChromeDriver(Driver):
    def __init__(self) -> None:
        self.driver = self.initialize()

    def initialize(self) -> WebDriver:
        capabilities = DesiredCapabilities.CHROME.copy()
        capabilities["platform"] = "LINUX"
        capabilities["version"] = "107.0"
        driver = webdriver.Remote(
            desired_capabilities=capabilities, command_executor=SELENIUM_GRID_URL
        )
        driver.implicitly_wait(5)
        return driver

    def close(self) -> None:
        self.driver.close()


class LocalChromeDriver(Driver):
    def __init__(self) -> None:
        self.driver = self.initialize()

    def initialize(self) -> WebDriver:
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        return driver

    def close(self):
        self.driver.close()


class Actions:
    """Find element/elements"""

    def __init__(self, driver: WebDriver) -> None:
        self.driver = driver

    def find(self, by, locator):
        return self.driver.find_element(by, locator)

    def finds(self, by, locator):
        return self.driver.find_elements(by, locator)

    def send_key(self, by, locator):
        self.find(by, locator).click()

    def get_text(self, by, locator) -> str:
        return self.find(by, locator).text
